﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCOA.Helper;
namespace MVCOA.Areas.Manage.Controllers
{
    public class Parentstr
    {
        public int parentName { get; set; }
        public string text { get; set; }  
    }
    public class Cate_SecondController : Controller
    {
        //
        // GET: /Manage/Cate_Second/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetParentdata()
        {
            //var sexData = [{ parentName: 1, text: '男' }, { parentName: 0, text: '女' }];
            IBLL.ICate_FisrtBLL first = OperateContext.Current.BLLSession.ICate_FisrtBLL;
            List<MODEL.Cate_Fisrt> list = first.GetListBy(q => true).Select(s => s.ToPOCO()).ToList();
            List<Parentstr> list2 = new List<Parentstr>();
            foreach (MODEL.Cate_Fisrt s in list)
            {
                Parentstr p = new Parentstr() { parentName = s.id,text = s.name };
                list2.Add(p);
            }
            var gridData = new
            {
                Rows = list2,
                Total = 1
            };

            return Json(list2);
        }
        public ActionResult Getdata(FormCollection fc)
        {

            IBLL.ICate_SecondBLL second = OperateContext.Current.BLLSession.ICate_SecondBLL;
            List<MODEL.Cate_Second> list = new List<MODEL.Cate_Second>();
            string sortname = fc["sortname"];
            string sortorder = fc["sortorder"];
            if (sortname != null && sortorder != null)
            {
                if (sortorder == "asc")//xiao->da
                {
                    if (sortname == "id") { list = second.GetListBy(q => true).OrderBy(s => s.id).Select(s => s.ToPOCO2()).ToList(); }
                    if (sortname == "name") { list = second.GetListBy(q => true).OrderBy(s => s.name).Select(s => s.ToPOCO2()).ToList(); }
                    if (sortname == "isNav") { list = second.GetListBy(q => true).OrderBy(s => s.isNav).Select(s => s.ToPOCO2()).ToList(); }
                    if (sortname == "parentName") { list = second.GetListBy(q => true).OrderBy(s => s.firstId).Select(s => s.ToPOCO2()).ToList(); }
                }

                if (sortorder == "desc")
                {
                    if (sortname == "id") { list = second.GetListBy(q => true).OrderByDescending(s => s.id).Select(s => s.ToPOCO2()).ToList(); }
                    if (sortname == "name") { list = second.GetListBy(q => true).OrderByDescending(s => s.name).Select(s => s.ToPOCO2()).ToList(); }
                    if (sortname == "isNav") { list = second.GetListBy(q => true).OrderByDescending(s => s.isNav).Select(s => s.ToPOCO2()).ToList(); }
                    if (sortname == "parentName") { list = second.GetListBy(q => true).OrderByDescending(s => s.firstId).Select(s => s.ToPOCO2()).ToList(); }
                }

            }
            else
            {
                list = second.GetListBy(q => true).Select(s => s.ToPOCO2()).ToList();

            }
            var gridData = new
            {
                Rows = list,
                Total = 1
            };
            return Json(gridData);
        }
        [HttpPost]
        public void Setdata(FormCollection fc)
        {
            // IBLL.ICate_FisrtBLL first = OperateContext.Current.BLLSession.ICate_FisrtBLL;
            string names = fc["name"];
            int ids = Convert.ToInt32(fc["id"]);
            string parentIdNames = fc["parentName"];
            int isNavs = 0;
            try { isNavs = Convert.ToInt32(fc["isNav"]); }
            catch(Exception e){}
            //string navIdstr = fc["navId"];
            int navIds = 0;
            if (parentIdNames == "") { return; }
            else
            {
                navIds = OperateContext.Current.BLLSession.ICate_FisrtBLL.GetListBy(s => s.name == parentIdNames).Select(s => s.id).First();
            }
            if (ids == 0)
            {
                if (OperateContext.Current.BLLSession.ICate_FisrtBLL.GetListBy(s => s.name == names).ToList().Count() == 0)
                {

                    MODEL.Cate_Second s = new MODEL.Cate_Second() { name = names, firstId = navIds,isNav = isNavs,type = 2};
                    OperateContext.Current.BLLSession.ICate_SecondBLL.Add(s);
                  
                }
            }
            else
            {
                MODEL.Cate_Second s = new MODEL.Cate_Second() { id = ids, name = names, firstId = navIds ,isNav = isNavs};
                OperateContext.Current.BLLSession.ICate_SecondBLL.Modify(s, "name", "firstId","isNav");
            }
        }
        [HttpPost]
        public void Deldata(FormCollection fc)
        {
            int ids = Convert.ToInt32(fc["id"]);
            //OperateContext.Current.BLLSession.ICate_ThirdBLL.DelBy(s => s.secondId == ids);
            OperateContext.Current.BLLSession.ICate_SecondBLL.DelBy(s => s.id == ids);
        }
    }
}
